<?php

namespace App\Admin\Controllers;

use App\Models\Menu;

use App\Models\News;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;

class MenuController extends BaseController
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');
            $content->body($this->treeView()->render());
//            $content->body($this->grid());
        });
    }

    protected function treeView()
    {
        return Menu::tree();
    }
    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Menu::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->model()->orderBy('type', 'desc');
            $grid->column('title', '名称');
            $grid->column('type', '类型')->select([
                1 => '单页',
                2 => '模块',
                3 => '列表',
                4 => '导航',
            ]);
            $grid->column('isShow', '显示')->switch(config('admin.status'));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Menu::class, function (Form $form) {

            $show=request('show');
            if(!$show||strstr($show,'parentId'))
            $form->select('parentId', '上级')->options(function ($ids) {
                $data = Menu::find($ids);
                return $data ? $data->pluck('title', 'id') : [0=>'无'];
            })->ajax('/admin/menu/get')->default(0);
            if(!$show||strstr($show,'title'))
            $form->text('title', '标题')->rules('required');
            if(!$show||strstr($show,'url'))
            $form->text('url', 'url');
            if(!$show||strstr($show,'en_title'))
            $form->text('en_title');
            if(!$show||strstr($show,'sort'))
            $form->number('sort');
            if(!$show||strstr($show,'type'))
            $form->select('type', '类型')->options([
                1 => '单页',
                2 => '模块',
                3 => '列表',
                4 => '导航',
            ])->default(4);
            if(!$show||strstr($show,'listTpl'))
            $form->text('listTpl', '列表模版');
            if(!$show||strstr($show,'newsTpl'))
            $form->text('newsTpl', '详情模版');
            if(!$show||strstr($show,'newsEditShow'))
            $form->text('newsEditShow', '列表表单显示');
            if(!$show||strstr($show,'newsExtraEditShow'))
            $form->text('newsExtraEditShow', '扩展列表表单显示');
            if(!$show||strstr($show,'seoTitle'))
            $form->text('seoTitle', 'seoTitle');
            if(!$show||strstr($show,'seoDes'))
                $form->text('seoDes', 'seoDes');
            if(!$show||strstr($show,'seoKey'))
                $form->text('seoKey', 'seoKey');
            if(!$show||strstr($show,'isShow'))
            $form->switch('isShow', '是否显示')->states(config('admin.status'));
            if(!$show||strstr($show,'content'))
            $form->editor('content','内容');
        });
    }
}
